package com.hooper.executor.demo;

import cn.hutool.core.util.RandomUtil;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/03/02/21:25
 */
public class TestSingleThreadPool {
    @Test
    public void test1(){
        //空参创建
        //单线程执行
        ExecutorService es = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            es.submit(new MyTask());
        }
    }

    @Test
    public void test2(){
        //基于ThreadFactory创建；可对线程进行设置 如：名称，优先级等
        ExecutorService es = Executors.newSingleThreadExecutor(r ->
                new Thread(r,"my task" + RandomUtil.randomInt(1,100)));
        for (int i = 0; i < 10; i++) {
            es.submit(new MyTask());
        }
    }


}